package com.shemy.fileservice.utils;

import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * @Description MD5 工具类.
 * @Author Dzsom
 * @Date 2018-10-19 23:15
 * @Encoding UTF-8
 * @Version 1.0
 **/
public class MD5Util {

	/**
	 * 获取该输入流的MD5值
	 * @author Dzsom
	 * @date 2018-10-24 7:23
	 * @return java.lang.String
	 */
    public static String getMD5(InputStream is) throws NoSuchAlgorithmException, IOException {
        StringBuilder md5 = new StringBuilder();
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] dataBytes = new byte[1024];

        int nread;
        while ((nread = is.read(dataBytes)) != -1) {
            md.update(dataBytes, 0, nread);
        }
        byte[] mdbytes = md.digest();

        // convert the byte to hex format
        for (byte mdbyte : mdbytes) {
            md5.append(Integer.toString((mdbyte & 0xff) + 0x100, 16).substring(1));
        }
        return md5.toString();
    }

    /**
     * UUID
     * @author Dzsom
     * @date 2018-10-24 7:22
     * @return java.lang.String
     */

    public static String getUUID() {
        return UUID.randomUUID().toString().trim().replace("-","");
    }

}
